package com.lndsnx.ai.controller;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.lndsnx.entity.ActorFilms;
import jakarta.annotation.Resource;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.converter.ListOutputConverter;
import org.springframework.ai.converter.MapOutputConverter;
import org.springframework.core.convert.support.DefaultConversionService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("/ai/output")
public class OutputController {

    @Resource
    private ChatClient chatClient;

    @GetMapping("/entity")
    public ActorFilms entity(@RequestParam(value = "query") String query) {
        return chatClient.prompt("你是一个演员，请列出你所参演的电影")
                .user(query)
                .call()
                .entity(ActorFilms.class);
    }

    @GetMapping("/list")
    public List<String> list(@RequestParam(value = "query") String query) throws Exception {
        List<String> list = chatClient.prompt("你是一个演员，请列出你所参演的电影")
                .user(query)
                .call()
                .entity(new ListOutputConverter(new DefaultConversionService()));
        return list;
        // return new ObjectMapper().readValue(list, new TypeReference<List<ActorFilms>>() {});
    }

    @GetMapping("/map")
    public Map<String, Object> map(@RequestParam(value = "query") String query) throws Exception {
        return chatClient.prompt("你是一个演员，请列出你所参演的电影")
                .user(query)
                .call()
                .entity(new MapOutputConverter());
        // String json = chatClient.prompt("你是一个演员，请列出你所参演的电影")
        //         .user(query)
        //         .call()
        //         .entity(new MapOutputConverter()).toString();
        // return new ObjectMapper().readValue(json, new TypeReference<Map<String,ActorFilms>>() {});
    }
}
